﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Vim25Api;
using Wicresoft.EKA.VCenter;


namespace Wicresoft.EKA.VCenterAdapter
{
    public class Class1
    {
        public static void GetStorage(OptionSpec[] array)
        {

            ExtendedAppUtil app = ExtendedAppUtil.Initialize("Test", array);
            app.Connect();
            ManagedObjectReference cViewVMS = app.Service.CreateContainerView(app.ServiceContent.viewManager, app.ServiceContent.rootFolder,
                new string[] { "VirtualMachine" }, true);
            PropertyFilterSpec spec = new PropertyFilterSpec();

            spec.propSet = new PropertySpec[] { new PropertySpec() };
            spec.propSet[0].type = "VirtualMachine";
            spec.propSet[0].pathSet = new string[] { "storage" };

            SelectionSpec[] selectionSpecs = new SelectionSpec[] { new TraversalSpec() };

            spec.objectSet = new ObjectSpec[] { new ObjectSpec() };
            spec.objectSet[0].obj = cViewVMS;
            spec.objectSet[0].skip = true;
            spec.objectSet[0].selectSet = selectionSpecs;
        }


        public static void Get(OptionSpec[] array)
        {

            //ExtendedAppUtil app = ExtendedAppUtil.Initialize("Test", array);
            //app.Connect();

            ExtendedAppUtil app = ExtendedAppUtil.Initialize("Test", array);
            app.Connect();

            HostDatastoreBrowserSearchSpec _hostDatastoreBrowserSearchSpec = new HostDatastoreBrowserSearchSpec();
            _hostDatastoreBrowserSearchSpec.details = new FileQueryFlags() { fileOwner = true, fileOwnerSpecified = true, fileSize = true, fileType = true, modification = true };
            _hostDatastoreBrowserSearchSpec.query = new FileQuery[] { new VmSnapshotFileQuery() };

            ManagedObjectReference dataCenter = app.ServiceUtilV25.GetDecendentMoRef(null, "Datacenter", "DEV");
            ManagedObjectReference dataStore1 = app.ServiceUtilV25.GetMoReFromProp(dataCenter, "datastore", "datastore1");
            ManagedObjectReference dataStoreBrowser = app.ServiceUtilV25.GetMoReFromProp(dataStore1, "browser");

            ObjectContent[] objary = app.ServiceUtilV25.GetObjectProperties(null, dataStore1, new String[] { "summary" });
            DatastoreSummary ds = objary[0].propSet[0].val as DatastoreSummary;
            ManagedObjectReference taskmor = app.ServiceConnectionV25.Service.SearchDatastoreSubFolders_Task(dataStoreBrowser, "[" + ds.name + "]",
                _hostDatastoreBrowserSearchSpec);

            object[] result = app.ServiceUtilV25.WaitForValues(taskmor, new string[] { "info.state", "info.result" },
                  new string[] { "state" }, // info has a property - state for state of the task
                  new object[][] { new object[] { TaskInfoState.success, TaskInfoState.error } }
            );

            // Wait till the task completes.
            if (result[0].Equals(TaskInfoState.success))
            {
                ObjectContent[] objTaskInfo = app.ServiceUtilV25.GetObjectProperties(null, taskmor, new String[] { "info" });
                TaskInfo tInfo = (TaskInfo)objTaskInfo[0].propSet[0].val; ;
                Vim25Api.HostDatastoreBrowserSearchResults[] searchResult = (Vim25Api.HostDatastoreBrowserSearchResults[])tInfo.result;

                int len = searchResult.Length;
                for (int j = 0; j < len; j++)
                {
                    HostDatastoreBrowserSearchResults sres
                        = searchResult[j];
                    FileInfo[] fileArray = sres.file;
                    if (fileArray != null)
                    {
                        for (int z = 0; z < fileArray.Length; z++)
                        {
                            Console.WriteLine("Virtual Disks Files " + fileArray[z].path + fileArray[z].fileSize);
                        }
                    }
                    else
                    {
                        Console.WriteLine("No Thin-provisioned Virtual Disks Files found");
                    }
                }
            }
            else
            {
                Console.WriteLine("SearchDatastoreSubFolders Task couldn't be completed successfully");
            }

        }
    }
}
